1 package cz
.brmlab
.brmtron
.server
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.DataOutputStream
;
5 import java
.io
.InputStreamReader
;
6 import java
.net
.Socket
;
8 import java
.util
.Scanner
;
11 public class BrmTronServerThread
extends Thread
{
15 private Player player
;
18 public BrmTronServerThread(Socket sock
, int id
){
19 super("BrmTronServerThread");
22 this.player
= new Player();
28 double[] loc
= new double[2];
30 DataOutputStream out
= new DataOutputStream(sock
.getOutputStream());
31 BufferedReader in
= new BufferedReader(new InputStreamReader(sock
.getInputStream()));
33 player
.setActive(true);
35 while((s
=in
.readLine()) != null) {
36 if(s
.equalsIgnoreCase("die")){
37 player
.setActive(false);
39 if(s
.equals("quit")) {
42 Scanner sc
= new Scanner(s
);
43 for(int i
=0;sc
.hasNextDouble();i
++) {
44 loc
[i
] = sc
.nextDouble();
46 player
.setCoords(loc
);
47 BrmTronServer
.updatePlayer(id
, player
);
48 out
.writeBytes(parsePlayers(BrmTronServer
.getPlayers()));
54 } catch(Exception e
) {
55 System
.err
.println(e
);
60 private String
parsePlayers(Map
<Integer
, Player
> players
) {
61 String ret
= new String();
62 Set
<Integer
> keys
= players
.keySet();
65 double[] coords
= players
.get(i
).getCoords();
66 boolean active
= players
.get(i
).isActive();
67 ret
+= "ID: " + i
+ ", Long: " + coords
[0] + ", Lat: " + coords
[1] + ", Active: " + active
+ "\n";